package it.polimi.traveldream.web.beans.administrator;

import it.polimi.traveldream.ejbclient.dtos.UserDTO;

import it.polimi.traveldream.ejbclient.eaos.AccountServiceEAO;
import it.polimi.traveldream.utils.enums.UserRole;
import it.polimi.traveldream.utils.exceptions.NotUniqueUserException;
import it.polimi.traveldream.web.beans.shared.FacesContextHandler;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="addEmployee")
@RequestScoped
public class AddEmployeeBean {
	@EJB
	private AccountServiceEAO aService;
	private UserDTO employee;
	
	public AddEmployeeBean() {
	}
	
	@PostConstruct
	public void init() {
		employee = new UserDTO();
		employee.setRole(UserRole.EMPLOYEE);
	}
	
	public UserDTO getEmployee() {
		return employee;
	}
	
	public void setEmployee(UserDTO employee) {
		this.employee = employee;
	}
	
	public String addEmployee() {
		try {
			aService.registerUser(employee);
			return "index";
		} catch (NotUniqueUserException e) {
			FacesContextHandler.produceFacesMessage(null,FacesContext.getCurrentInstance(),
					FacesMessage.SEVERITY_ERROR, "The email you entered is owned by another user. Please enter another one.");
			return "addEmployee";
		}
	}
	
}
